/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.atjava.jcrt.core.version.Version;
import com.atjava.jcrweb.Constants;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.ContentInfo;
import com.atjava.jcrweb.info.UserInfo;
import com.atjava.jcrweb.manager.ContentManager;
import com.atjava.jcrweb.manager.FileManager;
import com.atjava.jcrweb.manager.PackingToFlexHelper;

/**
 * 
 * 
 * @author biggie
 * @version $Revision: 1.11 $ $Date: 2008/07/24 01:29:52 $
 */
public class ContentFlexServlet extends FlexBaseServlet {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String process(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata)
			throws IOException, ServletException , JcrWebException , Exception {

		String type= getData(inputdata, "type");
		
		if(type.equals("createcon")){
			return createCon(request,response,inputdata);
		}
		if(type.equals("updatecon")){
			return updateCon(request,response,inputdata);
		}
		if(type.equals("delcon")){
			return delCon(request,response,inputdata);
		}
		if(type.equals("versionlst")){
			return versionList(request,response,inputdata);
		}
		return null;
	}
	private String versionList(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{
		ContentManager manager = ContentManager.getInstance();
		Version v = manager.getContentVersion(getData(inputdata,"path"),Long.parseLong(getData(inputdata,"id")));
		List list = v.getAllVersion();
		PackingToFlexHelper helper = new PackingToFlexHelper();
		StringBuffer sb = new StringBuffer();
		if(list!=null&&list.size()>0){
			helper.setResultSize(String.valueOf(list.size()));
			for(int i=0;i<list.size();i++){
				ContentInfo conInfo = (ContentInfo)list.get(i);
				sb.append("<"+(i+1)+">");
				sb.append("<data>");
				sb.append("<ver>"+(i+1)+"</ver>");
				sb.append(conInfo.toXmlString());
				sb.append("<path>"+conInfo.getPath()+"</path>");
				sb.append("<isfile>true</isfile>");
				sb.append("<picpath>images/file.gif</picpath>");
				sb.append("</data>");
				sb.append("</"+(i+1)+">");
			}
			helper.setResultData(sb.toString());
		}
		return helper.toString();
	}
	
	private String createCon(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{
		ContentManager manager = ContentManager.getInstance();
		ContentInfo info = new ContentInfo();
		info.setName(getData(inputdata,"name"));
		info.setCreaterCode(getData(inputdata,"createrCode"));
		info.setCreaterName(getData(inputdata,"createrName"));
		info.setNote(getData(inputdata,"note"));
		info = getFileInfoByRequest(request,inputdata,info);
		manager.createContent(info, getData(inputdata,"path"));
		return null;
	}
	
	private String updateCon(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{
		ContentManager manager = ContentManager.getInstance();
		ContentInfo info = manager.getContent(getData(inputdata,"path")+"/"+getData(inputdata,"id"));
		info.setId(Long.parseLong(getData(inputdata,"id")));
		info.setName(getData(inputdata,"name"));
		HttpSession session = request.getSession();
		if(session.getAttribute(Constants.LOGIN_USER_SESSION_KEY)!=null){
			String userCode = (String)session.getAttribute(Constants.LOGIN_USER_SESSION_KEY);
			if(Constants.ADMIN_USERNAME.equals(userCode)){
				info.setCreaterCode(Constants.ADMIN_USERNAME);
				info.setCreaterName(Constants.ADMIN_USERNAME);
			}else{
				UserInfo userInfo = (UserInfo)session.getAttribute(Constants.LOGIN_USER_SESSION_INFO);
				info.setCreaterCode(userInfo.getUserCode());
				info.setCreaterName(userInfo.getUserName());
			}
		}
		info.setNote(getData(inputdata,"note"));
		info = getFileInfoByRequest(request,inputdata,info);
		manager.updateContent(info, getData(inputdata,"path"));
		return null;
	}
	private ContentInfo getFileInfoByRequest(HttpServletRequest request,
			Map<String, String[]> inputdata,ContentInfo info) 
	throws IOException, ServletException , JcrWebException , Exception {
		
		//File 1
		if(getData(inputdata,"fileName1").length() > 0){
			String fileName = getData(inputdata,"fileName1");
			if(FileManager.getFileIdInMemory(fileName)!=null){
				long fileId = Long.parseLong(FileManager.getFileIdInMemory(fileName));
				info.setFile1Id(fileId);
				info.setFile1Name(fileName);
				FileManager.clearFileIdMemory(fileName);
			}
		}else{
			info.setFile1Id(0);
			info.setFile1Name("");
		}

		//File 2
		if(getData(inputdata,"fileName2").length() > 0){
			String fileName = getData(inputdata,"fileName2");
			if(FileManager.getFileIdInMemory(fileName)!=null){
				long fileId = Long.parseLong(FileManager.getFileIdInMemory(fileName));
				info.setFile2Id(fileId);
				info.setFile2Name(fileName);
				FileManager.clearFileIdMemory(fileName);
			}
		}else{
			info.setFile2Id(0);
			info.setFile2Name("");
		}
		
		//File 3
		if(getData(inputdata,"fileName3").length() > 0){
			String fileName = getData(inputdata,"fileName3");
			if(FileManager.getFileIdInMemory(fileName)!=null){
				long fileId = Long.parseLong(FileManager.getFileIdInMemory(fileName));
				info.setFile3Id(fileId);
				info.setFile3Name(fileName);
				FileManager.clearFileIdMemory(fileName);
			}
		}else{
			info.setFile3Id(0);
			info.setFile3Name("");
		}
		
		//File 4
		if(getData(inputdata,"fileName4").length() > 0){
			String fileName = getData(inputdata,"fileName4");
			if(FileManager.getFileIdInMemory(fileName)!=null){
				long fileId = Long.parseLong(FileManager.getFileIdInMemory(fileName));
				info.setFile4Id(fileId);
				info.setFile4Name(fileName);
				FileManager.clearFileIdMemory(fileName);
			}
		}else{
			info.setFile4Id(0);
			info.setFile4Name("");
		}
		
		//File 5
		if(getData(inputdata,"fileName5").length() > 0){
			String fileName = getData(inputdata,"fileName5");
			if(FileManager.getFileIdInMemory(fileName)!=null){
				long fileId = Long.parseLong(FileManager.getFileIdInMemory(fileName));
				info.setFile5Id(fileId);
				info.setFile5Name(fileName);
				FileManager.clearFileIdMemory(fileName);
			}
		}else{
			info.setFile5Id(0);
			info.setFile5Name("");
		}
		//File 6
		if(getData(inputdata,"fileName6").length() > 0){
			String fileName = getData(inputdata,"fileName6");
			if(FileManager.getFileIdInMemory(fileName)!=null){
				long fileId = Long.parseLong(FileManager.getFileIdInMemory(fileName));
				info.setFile6Id(fileId);
				info.setFile6Name(fileName);
				FileManager.clearFileIdMemory(fileName);
			}
		}else{
			info.setFile6Id(0);
			info.setFile6Name("");
		}
		
		//File 7
		if(getData(inputdata,"fileName7").length() > 0){
			String fileName = getData(inputdata,"fileName7");
			if(FileManager.getFileIdInMemory(fileName)!=null){
				long fileId = Long.parseLong(FileManager.getFileIdInMemory(fileName));
				info.setFile7Id(fileId);
				info.setFile7Name(fileName);
				FileManager.clearFileIdMemory(fileName);
			}
		}else{
			info.setFile7Id(0);
			info.setFile7Name("");
		}
		
		//File 8
		if(getData(inputdata,"fileName8").length() > 0){
			String fileName = getData(inputdata,"fileName8");
			if(FileManager.getFileIdInMemory(fileName)!=null){
				long fileId = Long.parseLong(FileManager.getFileIdInMemory(fileName));
				info.setFile8Id(fileId);
				info.setFile8Name(fileName);
				FileManager.clearFileIdMemory(fileName);
			}
		}else{
			info.setFile8Id(0);
			info.setFile8Name("");
		}
		
		//File 9
		if(getData(inputdata,"fileName9").length() > 0){
			String fileName = getData(inputdata,"fileName9");
			if(FileManager.getFileIdInMemory(fileName)!=null){
				long fileId = Long.parseLong(FileManager.getFileIdInMemory(fileName));
				info.setFile9Id(fileId);
				info.setFile9Name(fileName);
				FileManager.clearFileIdMemory(fileName);
			}
		}else{
			info.setFile9Id(0);
			info.setFile9Name("");
		}
		
		//File 10
		if(getData(inputdata,"fileName10").length() > 0){
			String fileName = getData(inputdata,"fileName10");
			if(FileManager.getFileIdInMemory(fileName)!=null){
				long fileId = Long.parseLong(FileManager.getFileIdInMemory(fileName));
				info.setFile10Id(fileId);
				info.setFile10Name(fileName);
				FileManager.clearFileIdMemory(fileName);
			}
		}else{
			info.setFile10Id(0);
			info.setFile10Name("");
		}
		
		return info;
	}
	
	private String delCon(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception {
		ContentManager manager = ContentManager.getInstance();
		manager.deleteContent(getData(inputdata,"path"),Integer.valueOf(getData(inputdata,"id")) );
		return null;
	}
}
